import  random
import  pygame
#全局常量定义
SCREEN_RECT = pygame.Rect(0,0,480,700)          #游戏窗体的区域
FRAME_INTERVAL = 10                             #动画间隔帧数

HERO_BOMB_COUNT = 3                             #英雄默认炸弹数量

# 英雄默认初始位置
HERO_DEFAULT_MID_BOTTOM = (SCREEN_RECT.center,SCREEN_RECT.bottom - 90)

HERO_DEAD_EVENT = pygame.USEREVENT                #英雄牺牲事件
HERO_POWER_OFF_EVENT = pygame.USEREVENT + 1       #取消英雄无敌事件
HERO_FIRE_EVENT = pygame.USEREVENT + 2            #英雄发射子弹事件

THROW_SUPPLY_EVENT = pygame.USEREVENT +3   #投放道具事件
BULLET_ENHANCED_OFF_EVENT = pygame.USEREVENT + 4   #关闭子弹增强事件


class Label(pygame.sprite.Sprite):
    #文本标签精灵
    font_path = "./res/font/MarkerFelt.ttc"
    def __int__(self,text,size,color,*groups):
        """初始化方法

                :param text: 文本内容
                :param size: 字体大小
                :param color: 字体颜色
                :param groups: 要添加到的精灵组
        """
        super().__init__(*groups)
        self.font = pygame.font.Font(self.font_path,size)
        self.color = color
        self.image = self.font.render(text,True,self.color)
        self.rect = self.image.get_rect()
    def set_text(self,text):
        self.image = self.font.render(text,True,self.color)
        self.rect = self.image.get_rect()

class GameSprite(pygame.sprite.Sprite):
    """游戏精灵类"""
    res_path="./res/images/"  #图片资源路径
    def __int__(self,image_name,speed,*groups):
        """初始化方法

             :param image_name: 要加载的图片文件名
             :param speed: 移动速度，0 表示静止
             :param groups: 要添加到的精灵组，不传则不添加
        """
        super().__init__(*groups)
        self.image = pygame.image.load(self.res_path+image_name)
        self.rect = self.image.get_rect()
        self.speed = speed

        # 图像遮罩，可以提高碰撞检测的执行性能
        self.mask = pygame.mask.from_surface(self.image)
    def update(self, *args):
        """更新精灵位置，默认在垂直方向移动

                :param args:
        """
        self.rect.y += self.speed

class StatusButton(GameSprite):
    def __int__(self,image_names,*groups):
        super().__init__(image_names[0],0,*groups)
        #加载图像
        self.images = [pygame.image.load(self.res_path+name) for name in image_names]

    def switch_status(self,is_pause):

        self.image =self.images[1 if is_pause else 0]

class Background(GameSprite):
    def __int__(self,is_all,*groups):
        super().__init__("background.png",1,*groups)

        if is_all:
            self.rect.y = -self.rect.h  #设置到游戏窗口正上方
    def update(self, *args):
        super().update(*args) #向下运动

        if self.rect.y >= self.rect.h:
            self.recty = -self.rect.h

class Plane(GameSprite):
    #飞机类
    def __int__(self,hp,speed,value,wav_name,normal_names,hurt_name,destroy_names,*groups):
        """初始化方法

            :param hp: 生命值
            :param speed: 速度
            :param value: 得分
            :param wav_name: 音频文件名
            :param normal_names: 正常飞行图像名称列表
            :param hurt_name: 受伤图像文件名
            :param destroy_names: 被摧毁图像文件名
            :param groups: 要添加到的精灵组
        """
        super().__init__(normal_names[0],speed,*groups)

        #飞机属性
        self.hp = hp
        self.max_hp = hp
        self.value = value
        self.wav_name =wav_name

        self.normal_images = [pygame.image.load(self.res_path+name) for name in normal_names]

        self.normal_index = 0

        self.hurt_image = pygame.image.load(self.res_path + hurt_name)

        self.destroy_images = [pygame.image.load(self.res_path + name) for name in destroy_names]

        self.destroy_index = 0

    def reset_plane(self):
        """重置飞机"""
        self.hp = self.max_hp  # 生命值

        self.normal_index = 0  # 正常状态图像索引
        self.destroy_index = 0  # 被摧毁状态图像索引

        self.image = self.normal_images[0]  # 恢复正常图像
    def update(self, *args):
        # 如果第 0 个参数为 False，不需要更新图像，直接返回
        if not args[0]:
            return

        if self.hp == self.max_hp:
            self.image = self.normal_images[self.normal_index]
            count = len(self.normal_images)
            self.normal_index = (self.normal_index + 1) % count
        elif self.hp > 0:
            self.image = self.hurt_image
        else:
            if self.destroy_index < len(self.destroy_images):
                self.image = self .destroy_images[self.destroy_index]

                self.destroy_index += 1
            else:
                self.reset_plane()
